﻿/*-------------------------------------------------------------------------
* 命名空间: SiliconSelect.ViewModels/LoginViewModel
* 类       名: LoginViewModel
* 功      能:  LoginView的Model类
* 时      间:  2023/6/21 10:01:06
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using log4net.Repository.Hierarchy;
using SiliconSelect.Base;
using SiliconSelect.BLL;
using SiliconSelect.Common;
using SiliconSelect.DAL.DataEntity;
using SiliconSelect.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace SiliconSelect.ViewModels
{
    public class LoginViewModel : NotifyBase
    {
        // 错误消息对象,和控件绑定需要添加通知属性
        // 用户名和密码错误的时候的信息展示
        private string? _errorMsg;

        public string? ErrorMsg
        {
            get { return _errorMsg; }
            set { SetProperty(ref _errorMsg, value); }
        }

        // 用户信息Model
        public UserModel userModel { get; set; }

        // 登录按钮的命令
        public Command? LoginCommand { get; set; }
        private void DoLoginCommand(object obj)
        {

            this.ErrorMsg = string.Empty;
            if (string.IsNullOrEmpty(userModel.Username))
            {
                this.ErrorMsg = "用户名不能为空!";
                return;
            }

            if (string.IsNullOrEmpty(userModel.Password))
            {
                this.ErrorMsg = "密码不能为空!";
                return;
            }

            Task.Run(new Action(() =>
            {
                string debugInfo = "";
                try
                {
                    var result = LocalDataAccess.CheckUserInfo(userModel.Username, userModel.Password);
                    if (!result.State)
                    {
                        this.ErrorMsg = result.Message;
                        LogHelper.WriteLog(LogType.Fatal, this.ErrorMsg);
                        return;
                    }
                    // 如果user不为空,将其信息保存到全局变量中
                    UserEntity user = result.Data!;
                    GlobalValues.UserInfo = user;
                    // 登录窗口关闭,开启主窗口
                    Application.Current?.Dispatcher.Invoke(new Action(() =>
                    {
                        Window? window = (obj as Window);
                        if (window != null)
                        {
                            window.DialogResult = true;
                            debugInfo = $"登录成功,用户名: {user.Username},角色: {user.Password}";
                            LogHelper.WriteLog(LogType.Info, debugInfo);
                        }
                    }));

                }
                catch (Exception ex)
                {
                    this.ErrorMsg = ex.Message;
                    LogHelper.WriteLog(LogType.Exception, this.ErrorMsg);
                }
            }));
        }


        /// <summary>
        /// 构造函数
        /// </summary>
        public LoginViewModel()
        {
            this.userModel = new UserModel();
            this.LoginCommand = new Command(DoLoginCommand);
        }
    }
}
